﻿using EmitMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Core;
using WebSite.Data.NH.Repositories;
using WebSite.Model;
using WebSite.Web.Models;
using WebSite.Web.SessionAndFilter;

namespace WebSite.Web.Controllers.Admin
{
    [AuthorAttribute]
    public class CompanyController : Controller
    {
        private readonly ICompanyRepository companyr;
        private readonly IContactRepository contactr;

        public CompanyController(ICompanyRepository companyr, IContactRepository contactr)
        {
            this.companyr = companyr;
            this.contactr = contactr;
        }


        public ActionResult Index()
        {
            var companyId = Sessions.User.CompanyId;
            var company = companyr.GetCompany(companyId);
            ObjectsMapper<CompanyInfo, CompanyVM> mapper =
                           ObjectMapperManager.DefaultInstance.GetMapper<CompanyInfo, CompanyVM>();
            var result = mapper.Map(company);
            return View(result);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(CompanyVM cvm)
        {
            ObjectsMapper<CompanyVM, CompanyInfo> mapper =
                          ObjectMapperManager.DefaultInstance.GetMapper<CompanyVM, CompanyInfo>();
            var company = mapper.Map(cvm);

            companyr.UpdateCompany(company);

            return View();
        }

        public ActionResult ContactIndex()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Contacts(string address,string phone,string fax,string email,string linkman, int page = 1, int pageSize = 15)
        {
            var user = Sessions.User;

            GridData<ContactVM> data = new GridData<ContactVM>();

            ObjectsMapper<ContactInfo, ContactVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<ContactInfo, ContactVM>();

            var where = PredicateExtension.True<ContactInfo>();
            where = where.AndAlso(u => u.CompanyId == user.CompanyId);
            if (!string.IsNullOrEmpty(address))
            {
                where = where.AndAlso(u => u.Address.Contains(address.Trim()));
            }
            if (!string.IsNullOrEmpty(phone))
            {
                where = where.AndAlso(u => u.Phone.Contains(phone.Trim()));
            }
            if (!string.IsNullOrEmpty(fax))
            {
                where = where.AndAlso(u => u.Fax.Contains(fax.Trim()));
            }
            if (!string.IsNullOrEmpty(email))
            {
                where = where.AndAlso(u => u.Email.Contains(email.Trim()));
            }
            if (!string.IsNullOrEmpty(linkman))
            {
                where = where.AndAlso(u => u.LinkMan.Contains(linkman.Trim()));
            }

            var count = contactr.LoadCount(where);
            var list = contactr.LoadAll(where, page, pageSize, new OrderByExpression<ContactInfo, int>(u => u.Id, true));
            data.Rows = mapper.MapEnum(list).ToList();
            data.Total = count;
            return Json(data);
        }


        public ActionResult ContactEdit(int id =0)
        {
            ContactVM contact = null;
            if (id == 0)
            {
                contact = new ContactVM();
                contact.CompanyId = Sessions.User.CompanyId;
            }
            else
            {
                ObjectsMapper<ContactInfo, ContactVM> mapper =
                ObjectMapperManager.DefaultInstance.GetMapper<ContactInfo, ContactVM>();
                //ContactInfo ci = contactr.GetById(id);
                //contact = mapper.Map(ci);
            }
            return View(contact);
        }

        [HttpPost]
        public ActionResult ContactEdit(ContactVM cvm)
        {
            if (ModelState.IsValid)
            {
                ObjectsMapper<ContactVM, ContactInfo> mapper =
                  ObjectMapperManager.DefaultInstance.GetMapper<ContactVM, ContactInfo>();

                var user = Sessions.User;

                if (cvm.Id == 0)
                {
                    var a = mapper.Map(cvm);
                    bool res = contactr.AddContact(a);
                    return Json(res);
                }
                else
                {
                    var a = mapper.Map(cvm);
                    bool res = contactr.UpdateContact(a);
                    return Json(res);
                }

            }
            return View();
        }

        [HttpPost]
        public ActionResult DeleteContact(int id)
        {
            var b = false;
            b = contactr.DeleteContact(id);
            return Json(b);
        }
    }
}
